package org.jeecg.modules.travel.user.controller;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;



import org.jeecg.modules.travel.user.service.IUserService;
import org.jeecg.modules.travel.user.entity.TravelWechatUser;
import org.jeecg.modules.travel.util.WechatUtil;

 /**
 * @Description: 流程测试
 * @Author: jeecg-boot
 * @Date:   2019-05-14
 * @Version: V1.0
 */
@RestController
@RequestMapping("/travel/user")
@Slf4j
public class UserController {
    @Autowired
	private IUserService userService;

    @Value("${WX_MINI.appId}")
    public String appId;
    @Value("${WX_MINI.secret}")
    public String secret;


	/**
	  * 登录接口
	 * @param data
	 * @param req
	 * @return
	 */
	@PostMapping(value = "/login")
	public Result<JSONObject> login(@RequestBody JSONObject data,HttpServletRequest req) {
        Result<JSONObject> result = new Result<JSONObject>();
        String codes = data.getString("code");
        if( codes == "" || "".equals(codes) ){
            result.setSuccess(false);
            result.setMessage("code不正确");
            return result;
        }
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appId+"&secret="+secret+"&js_code="+codes+"&grant_type=authorization_code";
        String str = WechatUtil.httpRequest(url, "GET",null);
        JSONObject jsonObject = JSONObject.parseObject(str);
        String openid = jsonObject.getString("openid");
        if( openid == null || openid == "" || "".equals(openid) ){
            result.setSuccess(false);
            result.setMessage("登录失败，微信授权失败。");
            return result;
        }
        result = userService.Login(openid);
		return result;
	}

	/**
	  * 注册接口接口
	 * @param data
	 * @param req
	 * @return
	 */
	@PostMapping(value = "/register")
	public Result<String> register(@RequestBody JSONObject data,HttpServletRequest req) {
        Result<String> result = new Result<String>();
        String codes = data.getString("code");
        if( codes == "" || "".equals(codes) ){
            result.setSuccess(false);
            result.setMessage("code不正确");
            return result;
        }
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appId+"&secret="+secret+"&js_code="+codes+"&grant_type=authorization_code";
        String str = WechatUtil.httpRequest(url, "GET",null);
        JSONObject jsonObject = JSONObject.parseObject(str);
        String openid = jsonObject.getString("openid");
        if( openid == null || openid == "" || "".equals(openid) ){
            result.setSuccess(false);
            result.setMessage("注册失败，微信认证错误！");
            return result;
        }
        data.put("openId",openid);
        Integer ret = userService.insertUser(data);
        switch (ret) {
            case 0:
                result.setSuccess(true);
                result.setMessage("注册成功！");
                break;
            case 1:
                result.setSuccess(false);
                result.setMessage("您已经注册过了，请直接登录。");
                break;
            case 3:
                result.setSuccess(false);
                result.setMessage("手机号已被注册。");
                break;
            default:
                result.setSuccess(false);
                result.setMessage("注册失败！未知错误。");
                break;
        }
        return result;
	}
}



